﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace ODM_Code.Forms
{
    public partial class ManageAnalysisAreasForm : Form
    {
        private Dictionary<string, string> extraAreasDict;

        public ManageAnalysisAreasForm()
        {
            InitializeComponent();
            extraAreasDict = new Dictionary<string, string>();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            this.Hide();

            ODM_Code.Forms.AddAnalysisAreaForm aaaf = new AddAnalysisAreaForm();
            aaaf.Activate();
            aaaf.BringToFront();
            aaaf.ShowDialog();

            this.PopulateAreasList();
            this.Show();
        }

        private void deleteAreaButton_Click(object sender, EventArgs e)
        {
            string selectedName = this.areasListBox.SelectedItem.ToString();

            DialogResult dr = MessageBox.Show("Are you sure you want to remove " + selectedName + " from the list?", "Delete Area", MessageBoxButtons.YesNo);

            if (dr == DialogResult.Yes)
            {
                DialogResult dr2 = MessageBox.Show("Do you want to delete " + selectedName + " data?", "Delete Area", MessageBoxButtons.YesNo);
                if (dr2 == DialogResult.Yes)
                {
                    string pathToDelete = "C:\\magfire\\" + extraAreasDict[selectedName];
                    if (Directory.Exists(pathToDelete))
                    {
                        Directory.Delete(pathToDelete, true);
                    }
                }

                extraAreasDict.Remove(selectedName);

                string extraAreas = "";

                foreach (KeyValuePair<string, string> item in extraAreasDict)
                {
                    extraAreas += item.Key + ":" + item.Value + ";";
                }
                ConfigSettings.WriteSetting("extraAreas", extraAreas);

            }
            else if (dr == DialogResult.No)
            {
                return;
            }
            this.PopulateAreasList();
        }

        private void ManageAnalysisAreasForm_Load(object sender, EventArgs e)
        {
            this.PopulateAreasList();
        }

        private void PopulateAreasList()
        {
            this.areasListBox.Items.Clear();
            string extraAreas = ConfigSettings.ReadSetting("extraAreas");
            string[] splitAreas = extraAreas.Split(';');
            foreach (string s in splitAreas)
            {
                if (s.Length > 0)
                {
                    int divider = s.IndexOf(':');
                    string name = s.Substring(0, divider);
                    string path = s.Substring(divider + 1);
                    extraAreasDict[name] = path;
                    this.areasListBox.Items.Add(name);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
